<template>
	<view class="resume-list">
		<!-- 简历列表 -->
		<template v-for="item in resumes" :key="item.cid">
			<view class="box">
				<view class="flex-center">
					<view class="title">
						<image mode="widthFix" class="title-icon" src="/static/img/resume/resume.svg">
						</image>
						<text>{{item.job_name}}</text>
					</view>
					<view class="flex-center">
						<text @tap="edit(item.cid)" class="edit">编辑</text>
						<text @tap="del(item.cid)" class="del">删除</text>
					</view>
				</view>
			</view>
		</template>
		<!-- 新建简历按钮 -->
		<view class="submit">
			<button @tap="add" class="submit-btn" style="width:660rpx">新建简历</button>
		</view>
	</view>
</template>

<script setup>
	import {
		onShow
	} from "@dcloudio/uni-app"
	import {
		ref,
		inject
	} from "vue"
	import loginCheck from "/hooks/loginCheck.js"
	import {
		req,
		post
	} from "/hooks/req.js"
	import {
		decrypt,
		encrypt
	} from "@/hooks/encrypt.js"
	const $root = inject("$root")
	const resumes = ref([])
	let token

	onShow(() => {
		resumes.value = []
		token = ''
		if (loginCheck()) {
			token = decrypt(JSON.parse(uni.getStorageSync("userData")).token)
			getResume()
		}
	})

	const getResume = () => {
		req($root + `/curri/`, token).then(result => {
			resumes.value = result.data.data
		})
	}

	const del = (id) => {
		post($root + `/curri/delete/${id}/`, {}, token).then(result => {
			if (result.data.code == 200) {
				getResume()
				uni.showToast({
					title: '删除简历成功'
				})
			} else {
				uni.showToast({
					title: '删除简历失败',
					icon: "error"
				})
			}
		})
	}

	const edit = (id) => {
		if (loginCheck()) {
			uni.navigateTo({
				url: "/pages/resumeDetail/resumeDetail?id=" + id
			})
		}
	}
	
	const add = () => {
		if (loginCheck()) {
			uni.navigateTo({
				url: "/pages/resumeDetail/resumeDetail"
			})
		}
	}
</script>


<style scoped>
	.edit,
	.del {
		width: 50px;
		text-align: center;
		border-radius: 18px;
		padding: 5px;
	}

	.edit {
		background: #1296db;
		margin-right: 10px;
		color: white;
	}

	.del {
		background: orangered;
		color: white;
	}
</style>